<template>
    <div class="game-box" v-loading="loading">
        <div class="select-box">
            <el-select v-model="status" @change="statusChange">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="卸载了" :value="0"></el-option>
                <el-option label="已安装" :value="1"></el-option>
                <el-option label="已收藏" :value="2"></el-option>
            </el-select>
        </div>
        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
                fixed
                prop="id"
                label="ID">
            </el-table-column>
            <el-table-column
                width="150"
                label="游戏名称">
                <template slot-scope="scope">
                    <div class="game-icon" v-if="scope.row.gameIcon">
                        <img :src="scope.row.gameIcon" alt="">
                    </div>
                    <div class="table-btn" @click="goGameDetail(scope.row.gameId)">{{scope.row.gameName || '-'}}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="游戏厂商">
                <template slot-scope="scope">
                    <span>{{scope.row.gameFirm || '-'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.status === '0'" style="color: #F56C6C;">卸载了</span>
                    <span v-else-if="scope.row.status === '1'">已安装</span>
                    <span v-else style="color:#67C23A;">已收藏</span>
                </template>
            </el-table-column>
            <el-table-column
                label="游戏时长">
                <template slot-scope="scope">
                    <span>{{gameTimeInit(scope.row.gameDuration)}}</span>
                </template>
            </el-table-column>
            <el-table-column
                min-width="200"
                label="发布游戏点评">
                <template slot-scope="scope">
                    <div>
                        <b>评分：</b>
                        <span>{{scope.row.commentScore || '0'}}</span>
                    </div>
                    <div>
                        <b>内容：</b>
                        <span>{{scope.row.publishGameComment || '-'}}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- <el-table-column
                label="版本号">
                <template slot-scope="scope">
                    <span>{{scope.row.publishGameComment || '-'}}</span>
                </template>
            </el-table-column> -->
            <el-table-column
                label="推荐新人">
                <template slot-scope="scope">
                    <span v-if="scope.row.recommend">是</span>
                    <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column
                label="发布视频数">
                <template slot-scope="scope">
                    <span>{{scope.row.publishVideoCount || '-'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="回复游戏点评数">
                <template slot-scope="scope">
                    <span>{{scope.row.relayGameCommentCount || '-'}}</span>
                </template>
            </el-table-column>
        </el-table>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            tableData: [],
            loading: false,
            status: -1,
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0
        }
    },

    props: ['detail'],

    created () {
        if (this.detail && this.detail.userId) {
            this.getDetail()
        }
    },

    methods: {
        // 获取详情
        async getDetail () {
            if (this.loading) return
            this.loading = true
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                userId: this.detail.userId
            }
            if (this.status !== -1) {
                params.status = this.status
            }
            let res = await this.$http.get('/user/getUserGameRecord', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count || res.data.length
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        // 查看点评详情
        goTalkDetail (id) {
            this.$router.push({
                path: '/game/gameTalkDetail',
                query: {
                    id
                }
            })
        },

        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getDetail()
        },

        pageChange (val) {
            this.pageNum = val
            this.getDetail()
        },

        statusChange (val) {
            this.pageNum = 1
            this.getDetail()
        },

        objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0) {
                if (row.rowSapn) {
                    return {
                        rowspan: row.rowSapn,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                }
            }
        },

        gameTimeInit (val) {
            if (val) {
                let min = val % 60
                let hour = parseInt(val / 60)
                if (hour) {
                    return `${hour}小时${min}分钟`
                } else {
                    return `${min}分钟`
                }
            } else {
                return '-'
            }
        },

        // 查看游戏详情
        goGameDetail (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        }
    }
}
</script>

<style lang="scss" scoped>
    .game-box {
        .select-box {
            margin-bottom: 10px;
        }

        .point {
            display:inline-block;
            width:10px;
            height:10px;
            border-radius: 50%;
            background-color:#67C23A;
            margin-right:3px;
        }
        .red-point {
            background-color: #F56C6C;
        }
        .game-icon {
            width: 60px;
            height: 60px;
            > img {
                width: 100%;
                height: 100%;
            }
        }
        .table-btn {
            font-size: 13px;
            color: #409EFF;
            cursor: pointer;
            &:hover {
                text-decoration: underline;
            }
        }

        .tools {
            margin-top: 10px;
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
